<?php
namespace app\index\controller;
use think\Controller;
use think\request;
use think\Db;
use app\service\orderService;
use app\service\chargeRuleService;
use app\service\apiService;
use app\service\wechatService;
use printing\printing;
use think\facade\Log;
error_reporting(0);
//ini_set('default_socket_timeout',30);
class Order extends Controller{
    public function yzl(){
        $order_id=240;
        $data["order_id"]=$order_id;
        (new Callback())->fsockopen('http://www.aliwgzf.com/index/callback/orderpaymsg2',$data);
    }
    
    
    public function couponOrder(){
        $pay_id=request()->post("pay_id");
		$orderinfo=Db::table("car_order_query")->where("id",$pay_id)->find();
		if(empty($orderinfo)){
		    return json(['info' => $pay_id]);
		}
		$orderinfo["payType"]=3;
		$orderinfo["paytime"]=date("YmdHis");
		$orderinfo["amountRec"]=0;
		$orderinfo["parkingEndTime"]=date("YmdHis");
		$orderinfo["tradeno"]=date("YmdHis").rand(100000,999999);
		$orderinfo["reqNo"]=date("YmdHis")."-".rand(100000,999999);
		unset($orderinfo["id"]);
		unset($orderinfo["payStatus"]);
		$order_id=Db::table("car_order")->insertGetId($orderinfo);
        $data["order_id"]=(int)$order_id;
        (new Callback())->fsockopen('http://www.aliwgzf.com/index/callback/orderpaymsg',$data);
        
        return json(['order_no' => $order_id]);
    }
    public function couponOk(){
        return "缴费成功";
    }
    public function coupon(){
        $cam_id=request()->get("id");
	    $caminfo=Db::table("parking_camera")->where("id",$cam_id)->find();
	    if(empty($caminfo)){
	        return "相机信息错误";
	    }
	    $this->assign("caminfo",$caminfo);
	    
	    //查询客户端是否在线
	    $clientinfo=Db::table("client_login")->where("parkingNo",$caminfo["parkingNo"])->where("clientNo",$caminfo["clientNo"])->find();
	    //echo 2;die;
        //var_dump($clientinfo);die;
	    if(empty($clientinfo)){

	        return "客户端不在线";
	    }
	    if($clientinfo["connect_status"]=="OFF"){
	        return "客户端不在线";
	    }
	    
	    
	    $client=new \swoole_client(SWOOLE_SOCK_TCP);
        if(!$client->Connect("47.106.184.188",9500,5)){
            echo "errCode:".$client->errCode."\r\n";
            die("connect failed\r\n");
        }
        
        $msg='{"bizCode":"C1015","parkingNo":"'.$caminfo["parkingNo"].'","clientNo":"'.$caminfo["clientNo"].'","cam_id":"'.$cam_id.'"}';
        $client->send($msg."\r\n");
        //从服务端接收信息
        $data=$client->recv();
        if(!$data){
            return "查询失败,请稍后再试";
            die("recv failed \r\n");
        }
        $orderinfo=json_decode($data,true);
        if($orderinfo["bizCode"]=="B1016"){
            if($orderinfo["respCode"]!="10000"){
                return "<div style='font-size:60px;text-align:center;margin-top:50px'>".$orderinfo["respMsg"]."</div>";
            }
        }
        $client->close();
        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$orderinfo["parkingNo"])->find();
        
        $this->assign("parkinginfo",$parkinginfo);
        unset($orderinfo["respMsg"]);
        unset($orderinfo["respCode"]);
        unset($orderinfo["sign"]);
        unset($orderinfo["bizCode"]);
        $orderinfo["parkingName"]=$parkinginfo["parkingName"];
        
        $orderinfo["outChannelNo"]=$caminfo["id"];
        
        $pay_id=Db::name('car_order_query')->insertGetId($orderinfo);
        $this->assign("pay_id",$pay_id);
        
        $this->assign("orderinfo",$orderinfo);
        //var_dump($orderinfo);
        //关闭连接
        
	    return $this->fetch();
    }
    
    public function outpay2(){
        $cam_id=request()->get("id");
        
		$redirect_uri="https://www.aliwgzf.com/index/orderali2/outpay?cam_id=".$cam_id;
		//echo $redirect_uri;die;
		$url="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2017100709184374&scope=auth_user&redirect_uri=".$redirect_uri;
		
      	header("Location:".$url);
	    exit;
    }
    //停车客户端预支付
    public function prepay(){
        //判断是不是微信
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
		    $parkingNo=request()->get("parkingNo");
		    
		    $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3fd7e9c4152f4823&redirect_uri=https://www.aliwgzf.com/index/orderwx/carquery?parkingNo=".$parkingNo."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
		    //$url="https://www.aliwgzf.com/index/orderwx/outpay?cam_id=".$cam_id;
          	header("Location:".$url);
		    exit;
        }
        
        //判断是不是支付宝
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'AlipayClient') !== false) {
			
            $parkingNo=request()->get("parkingNo");
		    $redirect_uri="https://www.aliwgzf.com/index/orderali/carquery?parkingNo=".$parkingNo;
		    $url="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2017100709184374&scope=auth_user&redirect_uri=".$redirect_uri;
      	    header("Location:".$url);
      	    exit;
          	header("Location:https://www.aliwgzf.com/index/orderali/outpay?cam_id=".$cam_id);
		    exit;
		}
        
        
    }
	//出口扫码支付
	public function outpay(){
	     
        //判断是不是支付宝
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'AlipayClient') !== false) {
			
            $cam_id=request()->get("id");
		    $redirect_uri="https://www.aliwgzf.com/index/orderali/outpay?cam_id=".$cam_id;
		    $url="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2017100709184374&scope=auth_user&redirect_uri=".$redirect_uri;
      	    header("Location:".$url);
      	    exit;
          	header("Location:https://www.aliwgzf.com/index/orderali/outpay?cam_id=".$cam_id);
		    exit;
		}
		//判断是不是微信
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
		    $cam_id=request()->get("id");
		    
		    $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3fd7e9c4152f4823&redirect_uri=https://www.aliwgzf.com/index/orderwx/outpay?cam_id=".$cam_id."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
		    //$url="https://www.aliwgzf.com/index/orderwx/outpay?cam_id=".$cam_id;
          	header("Location:".$url);
		    exit;
        }
        
        //哪个都不是
        return "请使用支付宝、微信扫码";
	    
	}
	public function outpayinfo(){
	    $cam_id=request()->get("cam_id");
	    $caminfo=Db::table("parking_camera")->where("id",$cam_id)->find();
	    $this->assign("caminfo",$caminfo);
	    
	    $carPlateNo=request()->param("car_number");
	    $client=new \swoole_client(SWOOLE_SOCK_TCP);
        if(!$client->Connect("47.106.184.188",9500,5)){
            echo "errCode:".$client->errCode."\r\n";
            die("connect failed\r\n");
        }
        $msg='{"bizCode":"C1015","parkingNo":"'.$caminfo["parkingNo"].'","clientNo":"'.$caminfo["clientNo"].'","cam_id":"'.$cam_id.'"}';
        $client->send($msg."\r\n");
        //从服务端接收信息
        $data=$client->recv();
        if(!$data){
            die("recv failed \r\n");
        }
        echo $data."\n";
        //关闭连接
        $client->close();
	}
	
	
	

	
	private function jsondata($arr){
        $str='{';
        foreach($arr as $key =>$value){
            $str=$str.'"'.$key.'":"'.$value.'",';
        }
        $str = trim($str, ",");
        $str=$str.'}';
        return $str;
    }
    private function MakeSign($arr,$parkingKey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //return $string;
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $parkingKey;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    
    //格式化参数格式化成url参数(str)
    private function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= strtolower($k) . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
    
}